import * as jwt from 'jsonwebtoken';
import { config } from '../../config/config';

interface ISign {
    payload: object;
}

interface IVerify {
    token: string;
    secret: string;
}

interface IJWT {
    sign(info: ISign): string;
    verify(info: IVerify): Promise<any>;
}

class JWT implements IJWT {
    sign(info: ISign): string {
        let token = jwt.sign(info.payload, config.jwtSecret);
        return token;
    }

    async verify(info: IVerify): Promise<any> {
        return new Promise((resolve,reject) => {
            jwt.verify(info.token, config.jwtSecret, function(err,decoded){
                if(err || !decoded) reject(err);
                else resolve(decoded);
            });
        })
    }
}

export { JWT }